package org.testis.gui.mvc.controller.testdesign;

import org.testis.gui.mvc.controller.*;
import org.testis.gui.mvc.model.testdesign.TestCaseModel;
import org.testis.gui.mvc.view.testdesign.TestCaseView;
import org.testis.orm.labelhandler.LabelHandlerFactory;
import java.util.HashMap;
import org.orm.PersistentException;
import org.testis.orm.refinedclasses.TestCaseRefined;
import org.testis.orm.refinedclasses.TestDesignTreeNode;
import org.testis.orm.refinedclasses.TestDesignTreeRefined.TestDesignTreeParameters;
import org.testis.orm.testdesign.TestPartSaver;
import org.testis.utils.tree.TreeNode;

/**
 *
 * @author Cahit Emir Erel
 */
public class TestCaseController extends AbstractController implements SaveAble,
        Addable {

    public static final String SHOW_ADD_TEST_CASE = "TestCaseVisibility";
    public static final String CHANGE_NAME = "Name";
    public static final String CHANGE_DESCRIPTION = "Description";
    public static final String CHANGE_RUN_ALWAYS = "RunAlways";
    public static final String CHANGE_TIMEOUT = "TimeOut";
    public static final String CHANGE_INVOCATION_COUNT = "InvocationCount";
    public static final String CHANGE_SUCCESS_PERCENTAGE = "SuccessPercentage";
    public static final String CHANGE_THREAD_POOL_SIZE = "ThreadPoolSize";
    public static final String CHANGE_CRITICAL = "Critical";
    private TestDesignTreeNode m_selectedNode = null;

    public TestCaseController() {
        super();
        TestDesignControllerList.newInstance().setTestCaseController(this);
    }

    public void operationCloseWindow() {
        cleanAll();
    }

    public void operationNewTestCase() {
        initialize(false);
        m_selectedNode = TestDesignControllerList.newInstance().getTreeController().getSelectedTreeNode();
        TestDesignControllerList.newInstance().getLabelPairSettingsController().operationNewSettings();
        setModelProperty(SHOW_ADD_TEST_CASE, true);
    }

    public void initialize(boolean editMode) {
        TestCaseModel testCaseModel = new TestCaseModel();
        TestCaseView testCaseView = new TestCaseView(TestDesignControllerList.newInstance().
                getParentFrame(),
                true,
                editMode);
        super.addModel(testCaseModel);
        super.addView(testCaseView);
        testCaseView.pack();
    }

    public void operationEditTestCase(TestDesignTreeNode node) {
        if (node.getReference().getType() == null) {
            System.out.println("Not found reference type for test case");
            return;
        } else if (node.getReference().getType().getName() == null) {
            System.out.println(
                    "Not found reference type name for test case");
            return;
        }
        if (node.getReference().getType().getName().equals("TestCase")) {
            initialize(true);
            TestCaseRefined designpart = null;
            try {
                designpart = (TestCaseRefined) node.getObject();
            } catch (Exception ex) {
                System.out.println(ex.toString());
            }
            if (designpart == null) {
                System.out.println("Not found design part for test case");
                return;
            }
            setModelProperty(CHANGE_NAME,
                    designpart.getName());
            setModelProperty(CHANGE_DESCRIPTION,
                    designpart.getReference().getDescription());
            Integer timeout = node.getReference().getParameterIntValue(
                    TestDesignTreeParameters.Timeout.name());
            if (timeout != null) {
                setModelProperty(CHANGE_TIMEOUT,
                        timeout);
            }

            Boolean runAlways = node.getReference().getParameterBooleanValue(
                    TestDesignTreeParameters.RunAlways.name());
            if (runAlways != null) {
                setModelProperty(CHANGE_RUN_ALWAYS,
                        runAlways);
            }
            Boolean critical = node.getReference().getParameterBooleanValue(
                    TestDesignTreeParameters.Critical.name());
            if (critical != null) {
                setModelProperty(CHANGE_CRITICAL,
                        critical);
            }
            Byte successPercent = node.getReference().getParameterByteValue(
                    TestDesignTreeParameters.SuccessPercentage.name());
            if (successPercent != null) {
                setModelProperty(CHANGE_SUCCESS_PERCENTAGE,
                        successPercent);
            }
            Integer threadPoolSize = node.getReference().getParameterIntValue(
                    TestDesignTreeParameters.ThreadPoolSize.name());
            if (threadPoolSize != null) {
                setModelProperty(CHANGE_THREAD_POOL_SIZE,
                        threadPoolSize);
            }
            Integer invocCount = node.getReference().getParameterIntValue(
                    TestDesignTreeParameters.InvocationCount.name());
            if (invocCount != null) {
                setModelProperty(CHANGE_INVOCATION_COUNT,
                        invocCount);
            }

            TestDesignControllerList.newInstance().getLabelPairSettingsController().operationEditSettings(
                    node);

            m_selectedNode = node;
            setModelProperty(SHOW_ADD_TEST_CASE, true);
        }
    }

    public void operationAddTestCase(HashMap<String, Object> params) throws PersistentException {
        TestDesignTreeNode node = TestDesignControllerList.newInstance().
                getTreeController().getSelectedTreeNode();
        TestDesignControllerList.newInstance().getTreeController().
                operationHandleAdd(this,
                node,
                params);
    }

    public void operationSaveTestCase(HashMap<String, Object> params) {
        TestDesignControllerList.newInstance().getTreeController().
                operationHandleSaveClones(this,
                m_selectedNode,
                params);
    }

    @Override
    public boolean operationSave(HashMap<String, Object> params,
            TreeNode node,
            boolean dynamicSaveOnly) {
        return TestPartSaver.operationSave(params,
                (TestDesignTreeNode) node);
    }

    public void operationChangeName(String name) {
        setModelProperty(CHANGE_NAME,
                name);
    }

    public void operationChangeDescription(String description) {
        setModelProperty(CHANGE_DESCRIPTION,
                description);
    }

    public void operationChangeTimeout(String timeout) {
        setModelProperty(CHANGE_TIMEOUT,
                Integer.parseInt(timeout));
    }

    public void operationChangeRunAlways(String runAlways) {
        setModelProperty(CHANGE_RUN_ALWAYS,
                Boolean.parseBoolean(runAlways));
    }

    public void operationChangeSuccessPercentage(String successPercentage) {
        setModelProperty(CHANGE_SUCCESS_PERCENTAGE,
                Byte.parseByte(successPercentage));
    }

    public void operationChangeInvocationCount(String invocationCount) {
        setModelProperty(CHANGE_INVOCATION_COUNT,
                Integer.parseInt(invocationCount));
    }

    public void operationChangeThreadPoolSize(String threadPoolSize) {
        setModelProperty(CHANGE_THREAD_POOL_SIZE,
                Integer.parseInt(threadPoolSize));
    }

    public void operationChangeCritical(String critical) {
        setModelProperty(CHANGE_CRITICAL,
                Boolean.parseBoolean(critical));
    }

    @Override
    public TestDesignTreeNode operationAddObject(HashMap<String, Object> params,
            TreeNode node, int childIndex) {
        TestDesignTreeNode nodeToAdd = TestPartSaver.operationAdd(params,
                (TestDesignTreeNode) node, childIndex, "TestCase");
        return nodeToAdd;
    }

    @Override
    public boolean isValidSave(HashMap<String, Object> params, TreeNode node) {
        return true;
    }

    @Override
    public boolean isValidAdd(HashMap<String, Object> params, TreeNode node) {
        return true;
    }
}
